package com.useeinfo.oa.modules.maintaining.biz;

import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.FacilityBiz;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.base.entity.Facility;
import com.useeinfo.oa.modules.maintaining.dao.FacilityMaintainDao;
import com.useeinfo.oa.modules.maintaining.entity.FacilityMaintain;
import com.useeinfo.oa.modules.maintaining.entity.MaintainData;
import com.useeinfo.oa.modules.maintaining.entity.MaintainTechnology;
import com.useeinfo.oa.modules.maintaining.entity.PlannerOrder;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Date: 2018/12/28 13:00
 */
@Service("facilityMaintainBiz")
public class FacilityMaintainBiz extends CrudBiz<FacilityMaintainDao, FacilityMaintain> {

    @Autowired
    private PlannerOrderBiz plannerOrderBiz;
    @Autowired
    private FacilityBiz facilityBiz;
    @Autowired
    private MaintainTechnologyBiz maintainTechnologyBiz;

    public List<FacilityMaintain> findByFacilityId(Long facilityId,String serial){
        return dao.findByFacilityId(facilityId,serial);
    }

    /**
     * 保存计划工单的id,设备——工艺list
     * @param facilityMaintainIds
     * @param plannerOrder
     * @return
     */
    public Set<FacilityMaintain> findByIds(String facilityMaintainIds, PlannerOrder plannerOrder){
        net.sf.json.JSONArray jsonArray= net.sf.json.JSONArray.fromObject(facilityMaintainIds);
        Set<FacilityMaintain> facilityMaintainSet=new HashSet<>();
        for(Object id:jsonArray){
            FacilityMaintain facilityMaintain=dao.findModel(StringConverters.null2Long(id));
            facilityMaintain.setPlannerOrder(plannerOrder);
            facilityMaintain.setTempVal(false);
            dao.add(facilityMaintain);
            facilityMaintainSet.add(facilityMaintain);
        }
        return facilityMaintainSet;

    }

    /**
     * 保存设备数据
     * @param facilityIds
     * @param plannerId
     * @param serial
     * @param request
     * @return
     */
    public ResultDto saveFacility(String facilityIds, String plannerId, String serial, HttpServletRequest request){

        Enterprise enterprise=SessionKeyUtils.getSessionEnterprise(request);
        PlannerOrder plannerOrder = plannerOrderBiz.findModel(StringConverters.null2Long(plannerId));
        net.sf.json.JSONArray jsonArray = net.sf.json.JSONArray.fromObject(facilityIds);
        for (Object id : jsonArray) {
            FacilityMaintain facilityMaintain;
            Facility facility = facilityBiz.findModel(StringConverters.null2Long(id));
            List<FacilityMaintain> facilityMaintainList = dao.findByFacilityId(StringConverters.null2Long(id), serial);
            if (CollectionUtils.isNotEmpty(facilityMaintainList)) {
                facilityMaintain = facilityMaintainList.get(0);
            } else {
                facilityMaintain = new FacilityMaintain();
                facilityMaintain.setTempVal(Boolean.TRUE);
                facilityMaintain.setEnterprise(enterprise);
            }
            facilityMaintain.setPlannerOrder(plannerOrder);
            facilityMaintain.setSerial(serial);
            facilityMaintain.setSpec(facility.getType());
            facilityMaintain.setFacilityId(StringConverters.null2Long(id));
            facilityMaintain.setFacilityName(facility.getName());
            facilityMaintain.setFacilityCode(facility.getCode());
            facilityMaintain.setFacilityGroupId(facility.getFacilityGroup() != null ? facility.getFacilityGroup().getId() : null);
            facilityMaintain.setFacilityGroupName(facility.getFacilityGroup() != null ? facility.getFacilityGroup().getFacilityGroupName() : "");
            facilityMaintain.setAreaId(facility.getArea() != null ? facility.getArea().getId() : null);
            facilityMaintain.setAreaName(facility.getArea() != null ? facility.getArea().getAreaName() : "");
            facilityMaintain.setFacilityCatId(facility.getFacilityCat() != null ? facility.getFacilityCat().getId() : null);
            facilityMaintain.setFacilityCatName(facility.getFacilityCat() != null ? facility.getFacilityCat().getCatName() : "");
            if(facilityMaintain.getId()!=null){
                dao.add(facilityMaintain);
            }else{
                dao.update(facilityMaintain);
            }
        }
        return ResultUtil.success();
    }

    /**
     * 保存工艺数据
     * @param facilityMaintainIds
     * @param maintainId
     * @param plannerId
     * @return
     */
    public ResultDto saveMaintain(String facilityMaintainIds, String maintainId, String plannerId){
        MaintainTechnology maintainTechnology = maintainTechnologyBiz.findModel(StringConverters.null2Long(maintainId));
        for (String id : facilityMaintainIds.split(",")) {
            FacilityMaintain facilityMaintain = dao.findModel(StringConverters.null2Long(id));
            PlannerOrder plannerOrder = plannerOrderBiz.findModel(StringConverters.null2Long(plannerId));
            facilityMaintain.setPlannerOrder(plannerOrder);
            facilityMaintain.setMaintainId(maintainTechnology.getId());
            facilityMaintain.setMaintainName(maintainTechnology.getName());
            facilityMaintain.setSafety(maintainTechnology.getSafetyRequirement());
            facilityMaintain.setMaintainDataContent(maintainTechnology.getMaintainDataContent());
            dao.update(facilityMaintain);
        }
        return ResultUtil.success();

    }

    /**
     * 删除无效数据
     * @return
     */
    public void deleteUselessData() {
        Date yesterdayTimeStamp = DateTimeUtils.getBeginOfDay(new Date());
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("noId","随便写什么");
        queryParam.getSqlMap().put("beforeTimeStamp",DateTimeUtils.formatDateToStringWithTime(yesterdayTimeStamp));
        List<FacilityMaintain> facilityMaintainList = this.findList(queryParam);
        if(CollectionUtils.isNotEmpty(facilityMaintainList)){
            for(FacilityMaintain facilityMaintain : facilityMaintainList){
                this.delete(facilityMaintain.getId());
            }
        }
    }

    /**
     * app接口保存设备工艺数据
     * @param plannerId
     * @param facilityId
     * @param maintainId
     * @param enterprise
     */
    public FacilityMaintain saveFacilityMaintain(String plannerId,String facilityId,String maintainId,Enterprise enterprise){

        PlannerOrder plannerOrder = plannerOrderBiz.findModel(StringConverters.null2Long(plannerId));
        FacilityMaintain facilityMaintain=new FacilityMaintain();
        Facility facility = facilityBiz.findModel(StringConverters.null2Long(facilityId));
        if(facility==null){
            return null;
        }
        facilityMaintain.setPlannerOrder(plannerOrder);
        facilityMaintain.setEnterprise(enterprise);
        facilityMaintain.setSerial(plannerOrder.getSerial());
        facilityMaintain.setFacilityId(StringConverters.null2Long(facilityId));
        facilityMaintain.setFacilityName(facility.getName());
        facilityMaintain.setFacilityCode(facility.getCode());
        facilityMaintain.setFacilityGroupId(facility.getFacilityGroup() != null ? facility.getFacilityGroup().getId() : null);
        facilityMaintain.setFacilityGroupName(facility.getFacilityGroup() != null ? facility.getFacilityGroup().getFacilityGroupName() : "");
        facilityMaintain.setAreaId(facility.getArea() != null ? facility.getArea().getId() : null);
        facilityMaintain.setAreaName(facility.getArea() != null ? facility.getArea().getAreaName() : "");
        facilityMaintain.setFacilityCatId(facility.getFacilityCat() != null ? facility.getFacilityCat().getId() : null);
        facilityMaintain.setFacilityCatName(facility.getFacilityCat() != null ? facility.getFacilityCat().getCatName() : "");
        facilityMaintain.setSpec(facility.getType());
        //保存工艺数据
        MaintainTechnology maintainTechnology = maintainTechnologyBiz.findModel(StringConverters.null2Long(maintainId));
        if(maintainTechnology==null){
            return null;
        }
        facilityMaintain.setMaintainId(maintainTechnology.getId());
        facilityMaintain.setMaintainName(maintainTechnology.getName());
        facilityMaintain.setSafety(maintainTechnology.getSafetyRequirement());
        facilityMaintain.setMaintainDataContent(maintainTechnology.getMaintainDataContent());
        dao.add(facilityMaintain);
        return facilityMaintain;

    }

    /**
     * 复制新增保存设备工艺信息
     * @param facilityMaintain
     * @param serial
     */
    public void saveCopy(FacilityMaintain facilityMaintain,String serial){
        FacilityMaintain facilityMaintain1=new FacilityMaintain();
        facilityMaintain1.setAreaId(facilityMaintain.getAreaId());
        facilityMaintain1.setAreaName(facilityMaintain.getAreaName());
        facilityMaintain1.setMaintainDataContent(facilityMaintain.getMaintainDataContent());
        facilityMaintain1.setEnterprise(facilityMaintain.getEnterprise());
        facilityMaintain1.setMaintainName(facilityMaintain.getMaintainName());
        facilityMaintain1.setMaintainId(facilityMaintain.getMaintainId());
        facilityMaintain1.setFacilityCatId(facilityMaintain.getFacilityCatId());
        facilityMaintain1.setFacilityCatName(facilityMaintain.getFacilityCatName());
        facilityMaintain1.setFacilityCode(facilityMaintain.getFacilityCode());
        facilityMaintain1.setFacilityGroupCode(facilityMaintain.getFacilityGroupCode());
        facilityMaintain1.setFacilityGroupId(facilityMaintain.getFacilityGroupId());
        facilityMaintain1.setFacilityGroupName(facilityMaintain.getFacilityGroupName());
        facilityMaintain1.setFacilityId(facilityMaintain.getFacilityId());
        facilityMaintain1.setQRCode(facilityMaintain.getQRCode());
        facilityMaintain1.setFacilityName(facilityMaintain.getFacilityName());
        facilityMaintain1.setSpec(facilityMaintain.getSpec());
        facilityMaintain1.setTempVal(false);
        facilityMaintain1.setSerial(serial);
        facilityMaintain1.setSafety(facilityMaintain.getSafety());
        dao.update(facilityMaintain1);




    }

}
